Page({
    data:{
        date:'',//当前日期
        isincome:true,//判断是收入还是支出
        incomeList:[],//收入数据
        payList:[],//支出数据
        isinput:true,//是否输入
        totalDate:'',//汇总日期
        incomeTotal:0,//收入汇总
        payTotal:0,//支出汇总
        incomeTotalList:0,//收入总数
        payTotalList:0,//支出总数
    },
    click(e){//切换收入支出模块
        console.log(e)
        if(e.currentTarget.dataset.name=='income'){
            this.setData({
                isincome:true
            })
        }else{
            this.setData({
                isincome:false
            })
        }
    },
    add(){//切换输入状态
        this.setData({
            isinput:!this.data.isinput
        })
    },
    changeDate(e){//改变日期时
        console.log(e)
        let date=e.detail.value
        this.setData({date})
    },
    changeTotalDate(e){//改变汇总日期时
        let totalDate=e.detail.value
        this.setData({
            totalDate
        })
        this.sum()
    },
    confirm(e){//添加数据
        console.log(e)
        let je=Number(e.detail.value)
        let date=this.data.date
        let aa={'date':date,'money':je}
        let incomeList=this.data.incomeList
        let payList=this.data.payList
        let incomeTotalList=this.data.incomeTotalList
        let payTotalList=this.data.payTotalList
        //计算
        if(this.data.isincome){//收入
            incomeList.push(aa)
            this.setData({
                incomeList,
                incomeTotalList:incomeTotalList+je,
                isinput:false
            },
            function(){
                console.log(this.data.incomeList)
            })
        }else{//支出
            payList.push(aa)
            this.setData({
                payList,
                payTotalList:payTotalList+je,
                isinput:false,
            })
        }
        this.sum()
    },
    sum(){//计算汇总
        let date=this.data.totalDate.slice(0,7)
        let income=this.data.incomeList
        let pay=this.data.payList
        let incomeTotal=0
        let payTotal=0
        //收取汇总
        for(let i=0;i<income.length;i++){
            let tempDate=income[i]['date'].slice(0,7)
            if(tempDate==date){
                incomeTotal+=income[i]['money']
            }
        }
        //支出汇总
        for(let i=0;i<pay.length;i++){
            let tempDate=pay[i]['date'].slice(0,7)
            if(tempDate==date){
                payTotal+=pay[i]['money']
            }
        }
        this.setData({
            incomeTotal,payTotal
        })
        console.log(incomeTotal)
        console.log(payTotal)
    },
    onUnload(){
        console.log('onUnload')
        this.onHide()
    },
    onHide(){//把数据存到内存中
        console.log('onHide')
        let that=this
        let store=[
            that.data.incomeList,
            that.data.payList,
            that.data.incomeTotalList,
            that.data.payTotalList,
            that.incomeTotal,
            that.payTotal,
        ]
        console.log(store)
        wx.setStorage({
            data:store,
            key:'money'
        })
    },
    onLoad(option){
        console.log('onLoad')
        let that=this
        // 获取当前日期
        let date=new Date().toLocaleDateString().replaceAll('/','-')
        let totalDate=date.slice(0,7)
        that.setData({
            date,totalDate
        })
        wx.getStorage({//取出数据
            key:'money',
            success(e){
                console.log(e)
                that.setData({
                    incomeList:e.data[0],
                    payList:e.data[1],
                    incomeTotalList:e.data[2],
                    payTotalList:e.data[3],
                    incomeTotal:e.data[4],
                    payTotal:e.data[5],
                })
            },
            complete(){
                that.sum()
            }
        })
    }
})